
<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
    <div  @mouseleave="leaveIndex" @mouseenter="enterShow">
      <h2 class="all">全部商品分类</h2>
      
      <!-- 
        1.a=>router-link 会卡顿 每个router-link都有一个实例，占内存大
        2.给每个a使用push，同理有多少个a标签就会有多少个触发函数,会卡顿
        3.委托给父元素使用 push 不会卡顿，问题不知道点击的那个元素【a h3 em dd】。需要给a自定义命名
      -->
      <transition name='sorts' >
        <div class="sort" v-show="show">
          <div
            class="all-sort-list2"
            @click="goSearch"
          >
            <div class="item" v-for="(v1, index) in categoryList"
            :key="v1.categoryId">
              <h3
                @mouseover="changeIndex(index)"
                :class="{ cur: categoryIndex == index }"
              >
                <a
                  :data-categoryName="v1.categoryName"
                  :data-category1id="v1.categoryId"
                  >{{ v1.categoryName }}</a
                >
              </h3>
              <div class="item-list clearfix" :style="{ display: categoryIndex == index ? 'block' : 'none' }">
                <div
                  class="subitem"
                >
                  <dl class="fore" v-for="(v2) in v1.categoryChild"
                  :key="v2.categoryId">
                    <dt>
                      <a
                        :data-categoryName="v2.categoryName"
                        :data-category2id="v2.categoryId"
                        >{{ v2.categoryName }}</a
                      >
                    </dt>
                    <dd
                      v-for="(v3, index) in v2.categoryChild"
                      :key="v3.categoryId"
                    >
                      <em>
                        <a
                          :data-categoryName="v3.categoryName"
                          :data-category3id="v3.categoryId"
                          >{{ v3.categoryName }}</a
                        >
                      </em>
                    </dd>
                  </dl>
                </div>
              </div>
            </div>
          </div>
        </div>
      </transition>
      </div>
     <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
     
  </div>
</template>

<script>
import { mapState } from "vuex";
import { throttle } from "lodash";
export default {
  name: "TypeNav",
  data() {
    return {
      categoryIndex: -1, // 鼠标移动到哪一个
      show: true,
    };
  },
  watch:{
    categoryList:{
      handler(){
      },
      immediate:true
    }
  },
  methods: {
    enterShow() {
      this.show = true; // 这里只有search是隐藏的
    },
    // es5
    changeIndex: throttle(function (index) {
      this.categoryIndex = index;
    }, 50),
    // es6
    leaveIndex() {
      this.categoryIndex = -1;
      if (this.$route.path != "/home") {
        this.show = false; // 这里只有search是隐藏的
      }
    },
    goSearch(e) {
      // 只有自定义的data-才有dataset属性
      let { categoryname, category1id, category2id, category3id } =e.target.dataset;
      // categoryname存在，表示点击的a 标签
      if (categoryname) {
      //// 获取a标签
      //   // 整理路由跳转的参数
        let location = { name: "Search" }; //跳转路由name
        let query = { categoryName: categoryname }; //路由参数
        if (category1id) {
          query.category1id = category1id; //一级
        } else if (category2id) {
          query.category2id = category2id; //二级
        } else if (category3id) {
          query.category3id = category3id; //三级
        }
      //   //判断：如果路由跳转的时候，带有params参数，捎带脚传递过去
      
        if (this.$route.params) {
          location.params = this.$route.params;
          //动态给location配置对象添加query属性
          location.query = query;
          //路由跳转
          this.$router.push(location);
        }else{
          //动态给location配置对象添加query属性
          location.query = query;
          //路由跳转
          this.$router.push(location);
        }
      }
    },
  },
  mounted() {
    // 挂载完毕，可以向服务器发请求
    // 通知vuex发请求，获取数据，存储于仓库中
  },
  computed:{
    ...mapState({
    // 箭头函数可使代码更简练
    categoryList: (state) => state.home.categoryList || [],
    // num:(state) => state.home.num,
    // 传字符串参数 'count' 等同于 `state => state.count`
    // countAlias: 'count',

    // // 为了能够使用 `this` 获取局部状态，必须使用常规函数
    // countPlusLocalState (state) {
    //   return state.count + this.localCount
    // }
  })
  } ,
};
</script>

<style  lang="scss" scoped>
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
            &.cur {
              background: skyblue;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }
        }
      }
    }
    .sorts-enter{
      height: 0px;
      //  opacity: 0
    }
    .sorts-leave-to{
      height: 461px;
      //  opacity: 0.5;
    }
     // 定义动画时间、速率
    .sorts-enter-active {
      transition: all 0.5s linear;
      //  transition: opacity 5s
    }

  }
}
</style>